using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using AuthService.Domain.Tokens;

namespace AuthService.Application.Tokens;

/// <summary>
/// 令牌仓储接口
/// </summary>
public interface ITokenRepository : IRepository<Token, Guid>
{
    /// <summary>
    /// 根据访问令牌获取令牌
    /// </summary>
    Task<Token> GetByAccessTokenAsync(
        string accessToken,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据刷新令牌获取令牌
    /// </summary>
    Task<Token> GetByRefreshTokenAsync(
        string refreshToken,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据用户ID获取活跃令牌
    /// </summary>
    Task<List<Token>> GetActiveTokensByUserIdAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据客户端ID获取令牌
    /// </summary>
    Task<List<Token>> GetTokensByClientIdAsync(
        string clientId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 根据用户ID和客户端ID获取令牌
    /// </summary>
    Task<List<Token>> GetTokensByUserIdAndClientIdAsync(
        Guid userId,
        string clientId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取过期的令牌
    /// </summary>
    Task<List<Token>> GetExpiredTokensAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取已撤销的令牌
    /// </summary>
    Task<List<Token>> GetRevokedTokensAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 撤销用户的所有令牌
    /// </summary>
    Task RevokeAllTokensByUserIdAsync(
        Guid userId,
        string reason = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 撤销客户端的所有令牌
    /// </summary>
    Task RevokeAllTokensByClientIdAsync(
        string clientId,
        string reason = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 撤销用户的刷新令牌
    /// </summary>
    Task RevokeRefreshTokensByUserIdAsync(
        Guid userId,
        string reason = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 清理过期令牌
    /// </summary>
    Task<int> CleanupExpiredTokensAsync(
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取令牌统计
    /// </summary>
    Task<TokenStatisticsDto> GetTokenStatisticsAsync(
        Guid? userId = null,
        string clientId = null,
        CancellationToken cancellationToken = default);
}

/// <summary>
/// 令牌统计DTO
/// </summary>
public class TokenStatisticsDto
{
    /// <summary>
    /// 总令牌数
    /// </summary>
    public int TotalTokens { get; set; }

    /// <summary>
    /// 活跃令牌数
    /// </summary>
    public int ActiveTokens { get; set; }

    /// <summary>
    /// 过期令牌数
    /// </summary>
    public int ExpiredTokens { get; set; }

    /// <summary>
    /// 已撤销令牌数
    /// </summary>
    public int RevokedTokens { get; set; }

    /// <summary>
    /// 访问令牌数
    /// </summary>
    public int AccessTokens { get; set; }

    /// <summary>
    /// 刷新令牌数
    /// </summary>
    public int RefreshTokens { get; set; }

    /// <summary>
    /// 按类型分组统计
    /// </summary>
    public Dictionary<string, int> TokensByType { get; set; } = new();

    /// <summary>
    /// 按客户端分组统计
    /// </summary>
    public Dictionary<string, int> TokensByClient { get; set; } = new();
}